/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package station;

import java.nio.ByteBuffer;

/**
 * zuständig für fachliches event: "Nachricht eingegangen" um alles notwendige
 * zu erneuern
 *
 * @author safe
 */
public class StationUpdateWorker extends Thread {

    private final boolean loggAll;
    private long timestamp;
    private LoggerThread logger;
    private byte[] message;

    public StationUpdateWorker(long timestamp, byte[] message) {
        this.timestamp = timestamp;
        this.message = message;
        this.loggAll = false;
    }

    public StationUpdateWorker(long timestamp, byte[] message, boolean loggAll) {
        this.timestamp = timestamp;
        this.message = message;
        this.loggAll = loggAll;
    }

    private void log(String station, String msg) {
        logger = new LoggerThread(station, msg);
        logger.start();
    }

    private String extractMessagePayLoad(byte[] message) {
        byte[] stationInfo = new byte[14];
        System.arraycopy(message, 10, stationInfo, 0, 14);
        return new String(stationInfo);
    }

    private String extractMessageStationType(byte[] message) {
        if (message[24] == 'A') {
            return "A";
        } else if (message[24] == 'A') {
            return "B";
        } else {
            return "";
        }
    }

    private int extractMessageSlotNumber(byte[] message) {
        return (int) message[25];
    }

    private String extractMessageStationInfo(byte[] message) {
        byte[] stationInfo = new byte[10];
        System.arraycopy(message, 0, stationInfo, 0, 10);
        return new String(stationInfo);
    }

    private long extractMessageTimestamp(byte[] message) {
        ByteBuffer buffer = ByteBuffer.wrap(message);
        return buffer.getLong(26);
    }

    @Override
    public void run() {
        int slotNumber = this.extractMessageSlotNumber(message);
        DataSlotBelegung.setSlotReserved(slotNumber);
        String stationName = this.extractMessageStationInfo(message);
        String dataPayLoad = this.extractMessagePayLoad(message);
        String stationClass = this.extractMessageStationType(message);

        long sendTimeStamp = this.extractMessageTimestamp(message);


//        if (DataEmpfangeneNachrichten.collisionDetect(timestamp)) {
//
//            if (!DataEmpfangeneNachrichten.collisionWithUs(stationName)) {
//                DataSlotBelegung.setSlotFree(DataSlotBelegung.getOurNextSlot());
//                DataEmpfangeneNachrichten.setSendingCollision(true);
//            }
//        } else {

        long[] timestamps = DataEmpfangeneNachrichten.setLastMessageAsValid(stationName, stationClass, slotNumber, sendTimeStamp, timestamp);
        if (timestamps[0] != 0 && timestamps[1] != 0) {
//            if (DataAbweichungsZeit.getStationClass().equals("A")) {
                DataAbweichungsZeit.adaptTimeOfStation(timestamps[0], timestamps[1]);
//            } else {
//                // hier lassen wir die differenzzeit zu 100% auf unsere abweichungszeit einwirken
//                DataAbweichungsZeit.setDiffTime(DataAbweichungsZeit.getAbWeichungsZeit() + (timestamps[0] - timestamps[1]));
//            }
        }
//    }
        if (loggAll) {
            this.log(stationName, "DATA: " + dataPayLoad + " CLASS: " + stationClass + " NEXT SLOT: " + slotNumber + " EMPFANGSZEIT: " + this.timestamp + " SENDEZEITPUNKT: " + sendTimeStamp);
        }
    }
}
